<?php

class Admin_Table_Auction_AuctionExecuters extends Apk_Db_Table {

    protected $_name = 'auction_executant';
    protected $_primary = 'id';
    protected $_ERROR = '';

    public function GetError() {
        return $this->_ERROR;
    }

    public function GetDataList($page = 1, $sort=null, $page_count=null) {
        $sel = $this->getAdapter()->select()
                ->where($this->_name .".is_ghost = ?", 0)
		->where($this->_name .".is_removed = ?", 0)
                ->from($this->_name)
        //->joinLeft('countries', 'countries.id = cities.id_country', array('country' => 'name_'.$this->_lang))
        //->joinLeft('regions', 'regions.id = cities.id_region', array('region' => 'name_'.$this->_lang))
        ;

        if (!is_null($sort) && strlen($sort) > 0)
            $sel->order($sort);
        else
            $sel->order($this->_name . '.name');

        return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name)
                ->where($this->_name . '.id = ?', $id)
                ->joinLeft(array('add_account' => "accounts"), "add_account.id = " . $this->_name . ".add_user_id", array('add_user' => "concat(add_account.last_name,' ',add_account.first_name,' ',add_account.patronymic_name)"))
                ->joinLeft(array('modify_account' => "accounts"), "modify_account.id = " . $this->_name . ".modify_user_id", array('modify_user' => "concat(modify_account.last_name,' ',modify_account.first_name,' ',modify_account.patronymic_name)"));

        return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function insert(array $data, $user_id) {
        $data['add_user_id'] = $user_id;
        $data['add_date'] = new Zend_Db_Expr("now()");
        $data['license_due_date'] = new Zend_Db_Expr("now()");
        $data['license_start_date'] = new Zend_Db_Expr("now()");
        parent::insert($data);
        return $this->getAdapter()->lastInsertId($this->_name);
    }

    public function update(array $data, $id, $user_id) {
        $data['is_ghost'] = 0;
        $data['modify_user_id'] = $user_id;
        $data['modify_date'] = new Zend_Db_Expr("now()");
        $data['license_due_date'] = date("Y-m-d H:i:s", strtotime($data['license_due_date'] . ' 00:00:00'));
        $data['license_start_date'] = date("Y-m-d H:i:s", strtotime($data['license_start_date'] . ' 00:00:00'));

        return parent::update($data, 'id = ' . $id);
    }

    public function remove($items) {
        foreach ($items as $k => $id)
            parent::update(array('is_removed' => 1), 'id = ' . $id);

        return true;
    }

    /*     * ******************
     * **  Decorators  ***
     * ******************* */

    public function simpleDecorator($key, $value) {
        switch ($key) {
            case 'add_date':
            case 'modify_date':
                if ($value == "" || $value === null) {
                    $result[$key] = '';
                } else {
                    $date = new DateTime($value);
                    $result[$key] = $date->format('d.m.Y H:i');
                }
                break;
            case 'license_due_date':
            case 'license_start_date':
                if ($value == "" || $value === null) {
                    $result[$key] = '';
                } else {
                    $date = new DateTime($value);
                    $result[$key] = $date->format('d.m.Y');
                }
                break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}

?>